<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="学生姓名" prop="studentId"   v-hasPermi="['cer:record:check']">
                <!--        <el-input-->
                <!--          v-model="queryParams.studentId"-->
                <!--          placeholder="请输入学生id"-->
                <!--          clearable-->
                <!--          @keyup.enter="handleQuery"-->
                <!--          v-hasPermi="['system:record:edit']"-->
                <!--        />-->
                <el-select v-model="queryParams.studentId" placeholder="请选择学生" style="width: 150px" clearable >
                    <el-option v-for="student in studentList" :key="student.id"
                               :label="student.studentName" :value="student.id" />
                </el-select>
            </el-form-item>
            <!--      <el-form-item label="证书id" prop="cerId">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.cerId"-->
            <!--          placeholder="请输入证书id"-->
            <!--          clearable-->
            <!--          @keyup.enter="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <el-form-item label="证书编号" prop="cerNo">
                <el-input
                        v-model="queryParams.cerNo"
                        placeholder="请输入证书编号"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <!--      <el-form-item label="证书文件地址" prop="cerFilePath">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.cerFilePath"-->
            <!--          placeholder="请输入证书文件地址"-->
            <!--          clearable-->
            <!--          @keyup.enter="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <el-form-item label="指导老师" prop="instructorId"  v-hasPermi="['cer:record:add']">
                <!--        <el-input-->
                <!--          v-model="queryParams.instructorId"-->
                <!--          placeholder="请输入指导老师id"-->
                <!--          clearable-->
                <!--          @keyup.enter="handleQuery"-->
                <!--        />-->
                <el-select v-model="queryParams.instructorId" placeholder="请选择学业导师" style="width: 150px" clearable v-hasPermi="['cer:record:edit']">
                    <el-option v-for="academicAdvisor in academicAdvisorList" :key="academicAdvisor.userId"
                               :label="academicAdvisor.nickName" :value="academicAdvisor.userId" />
                </el-select>
            </el-form-item>
            <!--      <el-form-item label="证书获取时间" prop="cerObtainTime">-->
            <!--        <el-date-picker clearable-->
            <!--          v-model="queryParams.cerObtainTime"-->
            <!--          type="date"-->
            <!--          value-format="YYYY-MM-DD"-->
            <!--          placeholder="请选择证书获取时间">-->
            <!--        </el-date-picker>-->
            <!--      </el-form-item>-->
            <!--      <el-form-item label="创建者id" prop="createrId">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.createrId"-->
            <!--          placeholder="请输入创建者id"-->
            <!--          clearable-->
            <!--          @keyup.enter="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <!--      <el-form-item label="更新者id" prop="updaterId">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.updaterId"-->
            <!--          placeholder="请输入更新者id"-->
            <!--          clearable-->
            <!--          @keyup.enter="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['cer:item:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['cer:record:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['cer:record:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['cer:record:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!--      <el-table-column label="唯一标识id" align="center" prop="id" />-->
            <el-table-column label="学生姓名" align="center" >
                <template #default="scope">{{ scope.row.studentInfo.studentName }}</template>
            </el-table-column>
            <el-table-column label="证书名称" align="center" >
                <template #default="scope">{{ scope.row.cerInfo?scope.row.cerInfo.cerName:'' }}</template>
            </el-table-column>
            <el-table-column label="证书编号" align="center" prop="cerNo" />
            <el-table-column label="证书文件地址" align="center" prop="cerFilePath" />
            <el-table-column label="指导老师" align="center" prop="instructorName" />
            <el-table-column label="审批状态" align="center"  >
                <template #default="scope">
                    <dict-tag :options="tag_status" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="证书获取时间" align="center" prop="cerObtainTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.cerObtainTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!--      <el-table-column label="创建者id" align="center" prop="createrId" />-->
            <!--      <el-table-column label="更新者id" align="center" prop="updaterId" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" :disabled="scope.row.status !== '0'&&scope.row.status !== 0" @click="handleUpdate(scope.row)" v-hasPermi="['cer:record:edit']">修改</el-button>
<!--                    <el-button link type="primary" icon="Edit" :disabled="scope.row.status !== '0'&&scope.row.status !== 0" @click="handleCerUpdate(scope.row)" v-hasPermi="['cer:record:check']">审核</el-button>-->
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" :disabled="scope.row.status !== '0'&&scope.row.status !== 0"  v-hasPermi="['cer:record:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改学生获取证书记录对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="学生姓名" prop="studentId">
                    <el-input  :disabled="true" :value="myIntroduce?myIntroduce.student.studentName:null"/>
                </el-form-item>
                <el-form-item label="指导老师" prop="instructorId">
                    <el-input v-model="form.instructorId" :disabled="true" :value="myIntroduce?myIntroduce.instructor.nickName:null" />
                </el-form-item>
                <el-form-item label="证书名称" prop="cerId">
                    <!--          <el-input v-model="form.cerId" placeholder="请输入证书id" />-->
                    <el-select v-model="form.cerId" placeholder="请选择证书" style="width: 150px" clearable >
                        <el-option v-for="item in infoList" :key="item.id"
                                   :label="item.cerName" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="证书编号" prop="cerNo">
                    <el-input v-model="form.cerNo" placeholder="请输入证书编号" />
                </el-form-item>
                <el-form-item label="证书图片" prop="cerFilePath">
                    <!--          <el-input v-model="form.cerFilePath" placeholder="请输入证书文件地址" />-->
                    <image-upload v-model="form.cerFilePath" />
                </el-form-item>

                <el-form-item label="证书获取时间" prop="cerObtainTime">
                    <el-date-picker clearable
                                    v-model="form.cerObtainTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择证书获取时间">
                    </el-date-picker>
                </el-form-item>
                <!--          <el-form-item label="审核状态" prop="status">-->
                <!--              <el-select v-model="form.status" placeholder="请选择审核状态">-->
                <!--                  <el-option-->
                <!--                          v-for="dict in tag_status"-->
                <!--                          :key="dict.value"-->
                <!--                          :label="dict.label"-->
                <!--                          :value="dict.value"-->
                <!--                  ></el-option>-->
                <!--              </el-select>-->
                <!--          </el-form-item>-->
                <!--        <el-form-item label="创建者id" prop="createrId">-->
                <!--          <el-input v-model="form.createrId" placeholder="请输入创建者id" />-->
                <!--        </el-form-item>-->
                <!--        <el-form-item label="更新者id" prop="updaterId">-->
                <!--          <el-input v-model="form.updaterId" placeholder="请输入更新者id" />-->
                <!--        </el-form-item>-->
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <!-- 审核证书记录对话框 -->
        <el-dialog :title="title" v-model="cerOpen" width="500px" append-to-body>
            <el-form ref="recordRef" :model="cerForm" :rules="cerRules" label-width="80px">
                <el-form-item label="学生姓名" prop="studentId">
                    <el-input  :disabled="true" :value="myIntroduce?myIntroduce.student.studentName:null"/>
                </el-form-item>

                <el-form-item label="审核状态" prop="status">
                    <el-select v-model="cerForm.status" placeholder="请选择审核状态">
                        <el-option
                                v-for="dict in tag_status"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <!--        <el-form-item label="创建者id" prop="createrId">-->
                <!--          <el-input v-model="form.createrId" placeholder="请输入创建者id" />-->
                <!--        </el-form-item>-->
                <el-form-item label="原因" prop="reason">
                    <el-input v-model="cerForm.reason" placeholder="请输入原因" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitCheck">确 定</el-button>
                    <el-button @click="cancelCheck">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Record">
import { listRecord, getRecord, delRecord, addRecord, updateRecord,getIntroduce,checkRecord } from "@/api/cer/cerStudentRecord";
import {listStudent,getAdvisorList} from "@/api/student/student";
import {getUserProfile} from "@/api/system/user";
import { listInfo } from "@/api/cer/cerInfo";
const { proxy } = getCurrentInstance();
const { sys_status } = proxy.useDict('sys_status');
const recordList = ref([]);
const studentList = ref([]);
const academicAdvisorList = ref([]);
const user = ref({});
const myIntroduce = ref({});
const infoList = ref([]);
const open = ref(false);
const cerOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const tag_status = [
    {label:'待审核',value:"0"},
    {label:'审核成功',value:"1"},
    {label:'审核失败',value:"2"},
]
// console.log(sys_status)

const data = reactive({
    form: {},
    cerForm:{},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        cerId: null,
        cerNo: null,
        cerFilePath: null,
        instructorId: null,
        cerObtainTime: null,
        createrId: null,
        updaterId: null,
    },
    rules: {
        // studentId: [
        //   { required: true, message: "学生id不能为空", trigger: "blur" }
        // ],
        cerId: [
            { required: true, message: "证书id不能为空", trigger: "blur" }
        ],
        cerNo: [
            { required: true, message: "证书编号不能为空", trigger: "blur" }
        ],
        cerFilePath: [
            { required: true, message: "证书文件地址不能为空", trigger: "blur" }
        ],
        cerObtainTime: [
            { required: true, message: "证书获取时间不能为空", trigger: "blur" }
        ],
        // createrId: [
        //   { required: true, message: "创建者id不能为空", trigger: "blur" }
        // ],
        // updaterId: [
        //   { required: true, message: "更新者id不能为空", trigger: "blur" }
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ],
        // updateTime: [
        //   { required: true, message: "更新时间不能为空", trigger: "blur" }
        // ]
    },
    cerRules: {
        // studentId: [
        //   { required: true, message: "学生id不能为空", trigger: "blur" }
        // ],
        status: [
            { required: true, message: "审批状态不能为空", trigger: "blur" }
        ],
        // createrId: [
        //   { required: true, message: "创建者id不能为空", trigger: "blur" }
        // ],
        // updaterId: [
        //   { required: true, message: "更新者id不能为空", trigger: "blur" }
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ],
        // updateTime: [
        //   { required: true, message: "更新时间不能为空", trigger: "blur" }
        // ]
    }
});



const { queryParams, form, rules,cerForm } = toRefs(data);

/** 查询学生获取证书记录列表 */
function getList() {
    loading.value = true;
    listRecord(queryParams.value).then(response => {
        recordList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
};

//获得学生列表
function getStudentList() {
    loading.value = true;
    listStudent().then(response => {
        studentList.value = response.rows;
        loading.value = false;
    });
};

//获得当前登录用户信息
function getUser() {
    getUserProfile().then(response => {
        user.value = response.data;
        if(response.data.roles){
            const item = response.data.roles.filter(obj => obj.roleId === 6)
            if(item.length=1&&response.data.roles.length ==1){//用户是学生权限
                getMyIntroduce(response.data.userId)
            }else {
                loadAdvisorList(response.data.deptId);
            }
        }
    });
};

//获取学业导师列表
function loadAdvisorList(deptId) {
    getAdvisorList(deptId).then(response => {
        academicAdvisorList.value = response;
    });
}

//获取当前用户学业导师列表
function getMyIntroduce(studentId) {
    getIntroduce(studentId).then(response => {
        myIntroduce.value = response.data;
    });
}

/** 查询证书信息列表 */
function getCerList() {
    loading.value = true;
    listInfo().then(response => {
        infoList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 取消审批按钮
function cancelCheck() {
    cerForm.value = false;
    cerReset();
}

//审批重置
function cerReset() {
    cerForm.value = {
        id: null,
        status:0,
        reason: null,
    };
    proxy.resetForm("recordRef");
}
// 表单重置
function reset() {
    form.value = {
        studentId:myIntroduce.value?myIntroduce.value.student.id:null,
        instructorId:myIntroduce.value?myIntroduce.value.student.academicAdvisorId:null,
        id: null,
        // studentId: null,
        cerId: null,
        cerNo: null,
        cerFilePath: null,
        // instructorId: null,
        status:0,
        cerObtainTime: null,
        createrId: null,
        updaterId: null,
        createTime: null,
        updateTime: null
    };
    proxy.resetForm("recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加学生获取证书记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getRecord(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改学生获取证书记录";
    });
}

/** 审批按钮操作 */
function handleCerUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getRecord(_id).then(response => {
        cerForm.value = response.data;
        cerOpen.value = true;
        title.value = "审批学生获取证书记录";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["recordRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateRecord(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addRecord(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 提交按钮 */
function submitCheck() {
    proxy.$refs["recordRef"].validate(valid => {
        if (valid) {
            checkRecord(cerForm.value).then(response => {
                proxy.$modal.msgSuccess("修改成功");
                cerOpen.value = false;
                getList();
            });
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除学生获取证书记录编号为"' + _ids + '"的数据项？').then(function() {
        return delRecord(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('system/record/export', {
        ...queryParams.value
    }, `record_${new Date().getTime()}.xlsx`)
}

getList();
getStudentList();
getUser();
getCerList()
</script>
